package com.shell.governor.security.user.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.security.user.dao.PubSecurityPaymentDao;
import com.shell.governor.security.user.po.PubSecurityPayment;
import com.shell.governor.security.user.service.PubSecurityPaymentService;

import tk.mybatis.mapper.entity.Example;

@Service
public class PubSecurityPaymentServiceImpl extends PersistentServiceImpl<PubSecurityPayment>
		implements PubSecurityPaymentService {
	@Autowired
	private PubSecurityPaymentDao pubSecurityPaymentDao;

	public int modifyUserPayPassword(String newPassword, String oldPassword) {
		int returnValue = 0;
		Example example = new Example(PubSecurityPayment.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", CommonDataUtil.getUserId());
		criteria.andEqualTo("paymentPwd", oldPassword);

		List<PubSecurityPayment> list = this.pubSecurityPaymentDao.selectByExample(example);
		if (list.size() > 0) {
			PubSecurityPayment u = (PubSecurityPayment) list.get(0);
			u.setPaymentPwd(newPassword);
			u.setEditorId(CommonDataUtil.getUserId());
			u.setEditorName(CommonDataUtil.getUserName());
			returnValue = this.pubSecurityPaymentDao.updateByPrimaryKeySelective(u);
		}
		return returnValue;
	}

	public int saveUserPayPassword(String newPassword) {
		PubSecurityPayment pubSecurityPayment = new PubSecurityPayment();
		pubSecurityPayment.setSecurityId(SystemUtil.getUUID());
		pubSecurityPayment.setPaymentPwd(newPassword);
		pubSecurityPayment.setUserId(CommonDataUtil.getUserId());
		pubSecurityPayment.setUserName(CommonDataUtil.getUserName());
		pubSecurityPayment.setCreateTime(SysDateUtil.getSystemDateTime());
		return this.pubSecurityPaymentDao.insertSelective(pubSecurityPayment);
	}

	public int updateFindUserPayPassword(String newPassword) {
		Example example = new Example(PubSecurityPayment.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("userId", CommonDataUtil.getUserId());

		PubSecurityPayment pubSecurityPayment = new PubSecurityPayment();
		pubSecurityPayment.setPaymentPwd(newPassword);
		pubSecurityPayment.setUserId(CommonDataUtil.getUserId());
		pubSecurityPayment.setUserName(CommonDataUtil.getUserName());
		pubSecurityPayment.setEditorId(CommonDataUtil.getUserId());
		pubSecurityPayment.setEditorName(CommonDataUtil.getUserName());
		pubSecurityPayment.setEditTime(SysDateUtil.getSystemDateTime());
		return this.pubSecurityPaymentDao.updateByExampleSelective(pubSecurityPayment, example);
	}

	public int resetPayPassword(String userId, String newPassword) {
		PubSecurityPayment pubSecurityPayment = new PubSecurityPayment();
		pubSecurityPayment.setUserId(userId);
		pubSecurityPayment = (PubSecurityPayment) selectOne(pubSecurityPayment);
		if (pubSecurityPayment == null) {
			return -1;
		}
		pubSecurityPayment.setPaymentPwd(newPassword);
		return updateNotNull(pubSecurityPayment);
	}
}
